﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml.Linq;
using SCFHS.Data;
using System.Web.Services;
using System.Configuration;
using System.IO;


namespace SCFHS.Views
{
    public partial class CompleteOrder : SysBase
    {
        #region Definitions


        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.GetCurrent(this).RegisterPostBackControl(this.btnUpload);


            if (!IsPostBack)
            {
                BindDropDownControls();
                Page.Form.Attributes.Add("enctype", "multipart/form-data");
            }
            string orderID = string.Empty;
            if (Request.QueryString["orderID"] != null)
                orderID = Request.QueryString["orderID"] as string;

            if (!string.IsNullOrEmpty(orderID) && !IsPostBack)
            {
                order = controller.SelectFullOrderInfo(int.Parse(orderID), attachmentFiles: true);
                BindOrderMainInfo(order);
                mView.ActiveViewIndex = 1;
            }
        }

        protected void btnNextToOrder_Click(object sender, EventArgs e)
        {
            order = controller.SelectMainOrderInfo(txtOrderNumberToSearch.Text);

            if (order == null)
            {
                lblMessage.Text = "عفوا لا يوجد طلب بهذا الرقم";
                lblMessage.CssClass = "error";
                return;
            }

            if (order.orderState == Code.OrderStates.ReadyTocomplete)
                order = controller.SelectFullOrderInfo(order.orderID, attachmentFiles: true);
            else
            {
                lblMessage.Text = "الطلب في مرحلة لا تسمح بعملية الإستكمال";
                lblMessage.CssClass = "error";
                return;
            }

            lblMessage.Text = "";
            mView.ActiveViewIndex = 1;
            BindOrderMainInfo(order);
        }

        protected void btnUpdateOrder_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = Messages.UpdateOrderCantbeDone;
                lblMessage.CssClass = "error";
                return;
            }

            bool result = controller.UpdateMainOrder(int.Parse(hdfOrderNo.Value), txtpersonName.Text, txtOrderNumber.Text, controller.ConvertDate(txtDate.Text));
            if (!result)
            {
                lblMessage.Text = Messages.UpdateOrderFail;
                lblMessage.CssClass = "error";
                return;
            }

            lblMessage.Text = Messages.UpdateOrderSuccess;
            lblMessage.CssClass = "info";
        }

        protected void btnAddSummeryItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم يتم إضافة العنصر حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            txtSummeryItem.Text = txtSummeryItem.Text.Replace("[b]", "<b>").Replace("[/b]", "</b>");
            txtSummeryItem.Text = txtSummeryItem.Text.Replace("[/font]", "</font>").Replace("[font color='#", "<font color='#").Replace("'c]", "'>");
            bool result = controller.CreateNewSummeryPoint(orderId, txtSummeryItem.Text.Replace("[b]", "<b>").Replace("[/b]", "</b>"));

            if (!result)
            {
                lblMessageSummery.Text = Messages.SummeryItemAddFail;
                lblMessageSummery.CssClass = "info";
                return;
            }
            txtSummeryItem.Text = "";
            BindSummeryGrid(controller.ListFullSummeryPointsForOrder(orderId));
        }

        protected void btnAddAttachmentItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم يتم إضافة العنصر حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            txtAttachmentItem.Text = txtAttachmentItem.Text.Replace("[b]", "<b>").Replace("[/b]", "</b>");
            txtAttachmentItem.Text = txtAttachmentItem.Text.Replace("[/font]", "</font>").Replace("[font color='#", "<font color='#").Replace("'c]", "'>");
            bool result = controller.CreateNewAttachmentPoint(orderId, txtAttachmentItem.Text.Replace("[b]", "<b>").Replace("[/b]", "</b>"));

            if (!result)
            {
                lblMessageAttachments.Text = Messages.SummeryItemAddFail;
                lblMessageAttachments.CssClass = "info";
                return;
            }
            txtAttachmentItem.Text = "";
            BindAttachmentsGrid(controller.ListAttachmentPoints(orderId));
        }

        protected void btnAddRequestItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم يتم إضافة العنصر حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            txtRequestItem.Text = txtRequestItem.Text.Replace("[b]", "<b>").Replace("[/b]", "</b>");
            txtRequestItem.Text = txtRequestItem.Text.Replace("[/font]", "</font>").Replace("[font color='#", "<font color='#").Replace("'c]", "'>");
            bool result = controller.CreateNewRequestPoint(orderId, txtRequestItem.Text.Replace("[b]", "<b>").Replace("[/b]", "</b>"));

            if (!result)
            {
                lblMessageRequests.Text = Messages.SummeryItemAddFail;
                lblMessageRequests.CssClass = "info";
                return;
            }
            txtRequestItem.Text = "";
            BindRequestGrid(controller.ListFullRequestPointsForOrder(orderId));
        }

        protected void btnDetails_Click(object sender, EventArgs e) { }

        protected void btnBrowseFile_Click(object sender, EventArgs e)
        {

        }

        protected void drp_DataBound(object sender, EventArgs e)
        {
            DropDownList drp = ((DropDownList)sender);
            drp.Items.Insert(0, new ListItem("يرجي الإختيار من القائمة", "-1"));
        }

        protected void btnUpdateFullOrder_Click(object sender, EventArgs e)
        {
            bool result = controller.UpdateOrderFullInfo(int.Parse(hdfOrderNo.Value), txtSubject.Text, txtClass.Text, int.Parse(drpNationality.SelectedValue),
                int.Parse(rdBirthPlace.SelectedValue), int.Parse(drpCurrenCity.SelectedValue), int.Parse(rdSecondrySchool.SelectedItem.Value), txtIDNumber.Text,
                int.Parse(rdIDType.SelectedValue), txtWorkPlace.Text, int.Parse(rdWorkType.SelectedValue));
            if (!result)
            {
                lblMessage.Text = "حدث خطأ ولم يتم تحديث الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            lblMessage.Text = "تم تحديث الطلب بنجاح";
            lblMessage.CssClass = "info";
            order.orderID = int.Parse(hdfOrderNo.Value);
            order = controller.SelectFullOrderInfo(order.orderID, attachmentFiles: true);
            BindOrderMainInfo(order);
        }

        protected void btnUpdateOrderState_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = Messages.CompleteOrderFail;
                lblMessage.CssClass = "error";
                return;
            }
            order = controller.SelectFullOrderInfo(int.Parse(hdfOrderNo.Value));
            if (order == null)
            {
                lblMessage.Text = Messages.CompleteOrderFail;
                lblMessage.CssClass = "error";
                return;
            }
            if (order.personCurrentResidencePlace < 1 || order.personNationality < 1)
            {
                lblMessage.Text = Messages.CompleteOrderFail + "يرجي التأكد من حفظ بيانات الطلب الأساسية اولا قبل تحويل الطلب للطباعة";
                lblMessage.CssClass = "error";
                return;
            }
            bool result = controller.UpdateOrderStates(int.Parse(hdfOrderNo.Value), Code.OrderStates.Place, user.userID);

            if (!result)
            {
                lblMessage.Text = Messages.CompleteOrderFail;
                lblMessage.CssClass = "error";
                return;
            }

            mView.ActiveViewIndex = 0;
            txtOrderNumberToSearch.Text = "";
            lblMessage.Text = Messages.CompleteOrderSuccess;
            lblMessage.CssClass = "info";

        }

        protected void grdAttachmentFiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string attachID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                attachID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(attachID))
                Result = controller.DeleteAttachmentFiles(int.Parse(attachID));
            if (!Result)
            {
                lblUploadMessage.CssClass = "error";
                lblUploadMessage.Text = Messages.UploadDeleteFileFail;
                return;
            }
            try
            {
                string UploadPath = ConfigurationManager.AppSettings["UploadPath"].ToString();
                if (string.IsNullOrEmpty(UploadPath))
                {
                    lblUploadMessage.CssClass = "error";
                    lblUploadMessage.Text = Messages.UploadDeleteFileFail;
                    return;
                }
                string fileName = ((LinkButton)grdAttachmentFiles.Rows[e.RowIndex].FindControl("lblAttachmentPoint")).Text;
                UploadPath = Server.MapPath(UploadPath) + "/" + fileName;
                if (File.Exists(UploadPath))
                {
                    File.Delete(UploadPath);
                }
                else
                {
                    lblUploadMessage.CssClass = "error";
                    lblUploadMessage.Text = "تم حذف إرتباط الملف من قاعدة البيانات و حدث خطأ أثناء حذف الملف الفعلي";
                }
            }
            catch (Exception)
            {
                throw;
            }
            lblUploadMessage.CssClass = "info";
            lblUploadMessage.Text = Messages.UploadDeleteFileSuccess;
            BindAttachmentFile();
        }

        protected void grdAttachmentFiles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';";
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string uploadFolder = ConfigurationManager.AppSettings["UploadPath"].ToString();
            if (string.IsNullOrEmpty(uploadFolder))
            {
                lblUploadMessage.Text = Messages.UploadFolderNotDefined;
                lblUploadMessage.CssClass = "error";
                return;
            }
            string uploadFolderPath = Server.MapPath(uploadFolder) + "\\";
            string fullPath = string.Empty;
            string newFileName = string.Empty;
            string dublicateNewFileName = string.Empty;

            if (!fuAttach.HasFile)
            {
                lblUploadMessage.Text = Messages.UploadNoFile;
                lblUploadMessage.CssClass = "error";
                return;
            }
            try
            {
                newFileName = hdfOrderNo.Value + Path.GetExtension(fuAttach.FileName);
                newFileName = Code.AttachmentsType.OrderAttachment.ToString() + "-" + newFileName;
                fullPath = uploadFolderPath + newFileName;
                int count = 0;
                while (File.Exists(fullPath))
                {
                    count++;
                    dublicateNewFileName = "({0})" + newFileName;
                    dublicateNewFileName = string.Format(dublicateNewFileName, count.ToString());
                    fullPath = uploadFolderPath + dublicateNewFileName;
                }

                fuAttach.PostedFile.SaveAs(fullPath);
            }
            catch (Exception)
            {
                throw;
            }
            if (!string.IsNullOrEmpty(dublicateNewFileName))
                newFileName = dublicateNewFileName;
            bool result = controller.AddAttachmentToOrder(int.Parse(hdfOrderNo.Value), newFileName, (int)Code.AttachmentsType.OrderAttachment, user.userID);

            if (!result)
            {
                lblUploadMessage.Text = Messages.UploadFail;
                lblUploadMessage.CssClass = "error";
                return;
            }

            lblUploadMessage.Text = Messages.UploadSuccess;
            lblUploadMessage.CssClass = "info";
            BindAttachmentFile();
        }

        #region GridsEvents
        // Summery grid
        protected void grdSummery_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdSummery.EditIndex = -1;
            BindSummeryGrid();
        }

        protected void grdSummery_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string SummeryID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                SummeryID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(SummeryID))
                Result = controller.DeleteSummeryPoint(int.Parse(SummeryID));
            if (!Result)
            {
                lblMessageSummery.CssClass = "error";
                lblMessageSummery.Text = "حدث خطأ ولم يتم حذف النقطةالمطلوبة";
                return;
            }
            lblMessageSummery.CssClass = "info";
            lblMessageSummery.Text = "تم حذف النقطة بنجاح";
            //grCitites.DeleteRow(e.RowIndex);
            BindSummeryGrid();

        }

        protected void grdSummery_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdSummery.EditIndex = e.NewEditIndex;
            BindSummeryGrid();
        }

        protected void grdSummery_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string SummeryID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                SummeryID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(SummeryID))
                Result = controller.UpdateSummeryPoint(int.Parse(SummeryID), ((TextBox)grdSummery.Rows[e.RowIndex].FindControl("txtSummeryPoint")).Text);
            if (!Result)
            {
                lblMessageSummery.CssClass = "error";
                lblMessageSummery.Text = "حدث خطأ ولم يتم تحديث النقطة المطلوبة";
                return;
            }
            lblMessageSummery.CssClass = "info";
            lblMessageSummery.Text = "تم تحديث إسم النقطة بنجاح";
            grdSummery.EditIndex = -1;
            BindSummeryGrid();
        }

        // Request grid
        protected void grdRequest_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdRequest.EditIndex = -1;
            BindRequestGrid();
        }

        protected void grdRequest_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string RequestID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                RequestID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(RequestID))
                Result = controller.DeleteRequestPoint(int.Parse(RequestID));
            if (!Result)
            {
                lblMessageRequests.CssClass = "error";
                lblMessageRequests.Text = "حدث خطأ ولم يتم حذف النقطةالمطلوبة";
                return;
            }
            lblMessageRequests.CssClass = "info";
            lblMessageRequests.Text = "تم حذف النقطة بنجاح";
            //grCitites.DeleteRow(e.RowIndex);
            BindRequestGrid();

        }

        protected void grdRequest_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdRequest.EditIndex = e.NewEditIndex;
            BindRequestGrid();
        }

        protected void grdRequest_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string RequestID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                RequestID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(RequestID))
                Result = controller.UpdateRequestPoint(int.Parse(RequestID), ((TextBox)grdRequest.Rows[e.RowIndex].FindControl("txtRequestPoint")).Text);
            if (!Result)
            {
                lblMessageRequests.CssClass = "error";
                lblMessageRequests.Text = "حدث خطأ ولم يتم تحديث النقطة المطلوبة";
                return;
            }
            lblMessageRequests.CssClass = "info";
            lblMessageRequests.Text = "تم تحديث إسم النقطة بنجاح";
            grdRequest.EditIndex = -1;
            BindRequestGrid();
        }

        // Attachment grid
        protected void grdAttachments_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdAttachments.EditIndex = -1;
            BindAttachmentsGrid();
        }

        protected void grdAttachments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string AttachmentID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                AttachmentID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(AttachmentID))
                Result = controller.DeleteAttachmentPoint(int.Parse(AttachmentID));
            if (!Result)
            {
                lblMessageAttachments.CssClass = "error";
                lblMessageAttachments.Text = "حدث خطأ ولم يتم حذف النقطةالمطلوبة";
                return;
            }
            lblMessageAttachments.CssClass = "info";
            lblMessageAttachments.Text = "تم حذف النقطة بنجاح";
            //grCitites.DeleteRow(e.RowIndex);
            BindAttachmentsGrid();

        }

        protected void grdAttachments_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdAttachments.EditIndex = e.NewEditIndex;
            BindAttachmentsGrid();
        }

        protected void grdAttachments_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string AttachmentID = string.Empty;
            bool Result = false;
            if (e.Keys["ID"] != null)
                AttachmentID = e.Keys["ID"].ToString();
            if (!string.IsNullOrEmpty(AttachmentID))
                Result = controller.UpdateAttachmentPoint(int.Parse(AttachmentID), ((TextBox)grdRequest.Rows[e.RowIndex].FindControl("txtAttachmentPoint")).Text);
            if (!Result)
            {
                lblMessageRequests.CssClass = "error";
                lblMessageRequests.Text = "حدث خطأ ولم يتم تحديث النقطة المطلوبة";
                return;
            }
            lblMessageAttachments.CssClass = "info";
            lblMessageAttachments.Text = "تم تحديث إسم النقطة بنجاح";
            grdAttachments.EditIndex = -1;
            BindAttachmentsGrid();
        }
        #endregion

        #endregion

        #region Helper Methods

        void BindOrderMainInfo(Order ordr)
        {
            hdfOrderNo.Value = ordr.orderID.ToString();
            txtpersonName.Text = ordr.personName;
            txtDate.Text = ordr.CreationDate.ToShortDateString();
            txtOrderNumber.Text = ordr.orderNumber;
            BindAttachmentFile(ordr.orderAttachmentFiles);
            BindSummeryGrid(controller.ListFullSummeryPointsForOrder(order.orderID));
            BindRequestGrid(controller.ListFullRequestPointsForOrder(order.orderID));
            BindAttachmentsGrid(controller.ListAttachmentPoints(order.orderID));
            BindFullOrderDetails(order.orderID);
        }

        void BindFullOrderDetails(int orderID)
        {
            order = controller.SelectFullOrderInfo(orderID);
            if (order == null)
                return;
            txtSubject.Text = order.orderSubject;
            drpNationality.SelectedValue = order.personNationality.ToString();
            drpCurrenCity.SelectedValue = order.personCurrentResidencePlace.ToString();
            txtIDNumber.Text = order.personIDNumber;
            rdIDType.SelectedValue = order.personIDType.ToString();
            txtClass.Text = order.orderClassificationNumber;
            rdSecondrySchool.SelectedValue = order.personHighSchoolPlace.ToString();
            rdBirthPlace.SelectedValue = order.personBirthPlace.ToString();
            txtWorkPlace.Text = order.workPlaceName;
            rdWorkType.SelectedValue = order.workPlaceType.ToString();
        }

        void BindOrderByClassification(int orderID)
        {

        }
        #endregion

        #region Data Methods
        void BindDropDownControls()
        {
            drpCurrenCity.DataSource = controller.ListCities();
            drpCurrenCity.DataValueField = "ID";
            drpCurrenCity.DataTextField = "Name";
            drpCurrenCity.DataBind();

            drpNationality.DataSource = controller.ListNationalities();
            drpNationality.DataValueField = "ID";
            drpNationality.DataTextField = "Name";
            drpNationality.DataBind();
        }

        void BindAttachmentFile()
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم تتم العملية بشكل صحيح حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            BindAttachmentFile(controller.ListAttachmentFiles(orderId, Code.AttachmentsType.OrderAttachment));

        }
        void BindAttachmentFile(DataTable sourceDt)
        {
            if (sourceDt != null && sourceDt.Rows.Count > 0)
            {
                grdAttachmentFiles.DataSource = sourceDt;
                grdAttachmentFiles.DataBind();
            }
        }

        void BindAttachmentsGrid()
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم يتم إضافة العنصر حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            BindAttachmentsGrid(controller.ListAttachmentPoints(orderId));

        }
        void BindAttachmentsGrid(DataTable sourceDt)
        {
            if (sourceDt != null)
            {
                if (sourceDt.Rows.Count > 0)
                {
                    grdAttachments.DataSource = sourceDt;
                    grdAttachments.DataBind();
                }
            }
            else
                grdAttachments.DataBind();
        }

        void BindSummeryGrid()
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم يتم إضافة العنصر حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            BindSummeryGrid(controller.ListFullSummeryPointsForOrder(orderId));

        }
        void BindSummeryGrid(DataTable sourceDt)
        {
            if (sourceDt != null)
            {
                if (sourceDt.Rows.Count > 0)
                {
                    grdSummery.DataSource = sourceDt;
                    grdSummery.DataBind();
                }
            }
            else
                grdSummery.DataBind();
        }

        void BindRequestGrid()
        {
            if (string.IsNullOrEmpty(hdfOrderNo.Value))
            {
                lblMessage.Text = "حدث خطأ ولم يتم إضافة العنصر حيث ان رقم الطلب غير صحيح قم بإعادة البحث عن الطلب";
                lblMessage.CssClass = "error";
                return;
            }

            int orderId = int.Parse(hdfOrderNo.Value);
            BindRequestGrid(controller.ListFullRequestPointsForOrder(orderId));

        }
        void BindRequestGrid(DataTable sourceDt)
        {
            if (sourceDt != null)
            {
                if (sourceDt.Rows.Count > 0)
                {
                    grdRequest.DataSource = sourceDt;
                    grdRequest.DataBind();
                }
            }
            else
                grdRequest.DataBind();
        }
        #endregion

        [WebMethod]
        public static void CheckClassificationNumber(string classNumber)
        {
            if (string.IsNullOrEmpty(classNumber))
                return;
            SCFHS.Code.Controller controller = new Code.Controller();

            CompleteOrder completeorder = new CompleteOrder();
            GridView grd = new GridView();
            grd.DataSource = controller.SelectOrdersByClassificationNumber(classNumber);
            completeorder.grdOrderByClassification = grd;
            completeorder.grdOrderByClassification.DataBind();
        }

        [WebMethod]
        public static void CheckIDNumber(string IDNumber)
        {
            if (string.IsNullOrEmpty(IDNumber))
                return;
            SCFHS.Code.Controller controller = new Code.Controller();

            CompleteOrder completeorder = new CompleteOrder();
            GridView grd = new GridView();
            grd.DataSource = controller.SelectOrdersByClassificationNumber(IDNumber);
            completeorder.grdOrderByClassification = grd;
            completeorder.grdOrderByClassification.DataBind();
        }

        protected void upnlLoadClassi_Load(object sender, EventArgs e)
        {
            if (Request["__EVENTTARGET"] != null)
                if (Request["__EVENTTARGET"].Contains("loadClassi"))
                {
                    grdOrderByClassification.DataSource = controller.SelectOrdersByClassificationNumber(txtClass.Text);
                    if (grdOrderByClassification.DataSource != null)
                    {
                        grdOrderByClassification.Visible = true;
                        grdOrderByClassification.DataBind();
                        return;
                    }
                    grdOrderByClassification.Visible = false;
                }
                else if (Request["__EVENTTARGET"].Contains("loadById"))
                {
                    grdOrderByClassification.DataSource = controller.SelectOrdersByClassificationNumber(txtClass.Text);
                    if (grdOrderByClassification.DataSource != null)
                    {
                        grdOrderByClassification.Visible = true;
                        grdOrderByClassification.DataBind();
                        return;
                    }
                    grdOrderByClassification.Visible = false;
                }
        }
    }
}
